package com.wmh.baseservice.merchant.repository.impl;

import com.wmh.baseservice.common.redis.utils.RedisUtil;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.DateFormatTemplate;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.merchant.mapper.MerchantGroupGoodsMapper;
import com.wmh.baseservice.merchant.pojo.MerchantGroupGoods;
import com.wmh.baseservice.merchant.repository.MerchantGroupGoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 谢骏
 * @date 2022/9/12 13:45
 **/
@Repository
public class MerchantGroupGoodsRepositoryImpl implements MerchantGroupGoodsRepository {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MerchantGroupGoodsMapper merchantGroupGoodsMapper;

    @Override
    public void init() {
        List<MerchantGroupGoods> goodslist = merchantGroupGoodsMapper.selectIsNoOverdue();
        // todo 免宕机 , 获取保护 key , 根据保护key 获取 定时key , 若定时key 不存在 则表示在宕机过程中 拼团结束 调用相对于的结算

        if(CommUtil.checkNull(goodslist)){
            goodslist.forEach(i -> {
                Long id = i.getId();
                String endTime = i.getEndTime();
                if(CommUtil.checkNull(i.getId(),i.getEndTime())){
                    setTiming(id,DateFormatTemplate.paresDate(endTime));
                }else{
                    LogUtil.warn("一个团购商品时间监控初始化失败 -> id : {} , endTime : {}" , id, endTime);
                }
            });
            LogUtil.info("团购商品时间监控预热完毕...");
        }
    }

    @Override
    public void setTiming(Long bgId, LocalDateTime overTime) {
        LocalDateTime now = LocalDateTime.now(DateFormatTemplate.ZONE_SHANGHAI);
        /*是否设置的已经是过期是时间 或马上过期的时间*/
        if(now.isAfter(overTime) || now.isEqual(overTime)){
            return;
        }
        long interval = Duration.between(now, overTime).toMillis() / 1000;
        if(interval == 0){
            return;
        }
        String time = overTime.format(DateTimeFormatter.ofPattern(DateFormatTemplate.GROUP_GOODS_WRITE));
        redisUtil.set(KEY_PREFIX  + bgId + ":" + KEY_PLACEHOLDER, time, interval);
        redisUtil.set(KEY_PREFIX  + bgId + ":" + KEY_OVER_PROTECT ,time,interval + PROTECT_INTERVAL);
    }

    @Override
    public void rmTiming(Long bgId) {
        redisUtil.del(KEY_PREFIX  + bgId + ":" + KEY_PLACEHOLDER, KEY_PREFIX  + bgId + ":" + KEY_OVER_PROTECT);
    }

    @Override
    public boolean hasOverKey(Long bgId) {
        return redisUtil.hasKey(KEY_PREFIX  + bgId + ":" + KEY_PLACEHOLDER);
    }

    @Override
    public boolean hasProtectKey(Long bgId) {
        return redisUtil.hasKey(KEY_PREFIX  + bgId + ":" + KEY_OVER_PROTECT);
    }

    @Override
    public String getProtectKey(Long bgId) {
        return redisUtil.get(KEY_PREFIX  + bgId + ":" + KEY_OVER_PROTECT).toString();
    }
}
